4
4
.
.
1
1
.
.
3
3
M
M
a
a
p
p
V
V
i
i
e
e
w
w
(
(
w
w
r
r
a
a
p
p
p
p
e
e
r
r
)
)
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorial shows how to use a Map View.
SwiftUI doesn't support Map View so we have to use MapKit and wrap it inside UIViewRepresentable.
S
S
e
e
t
t
C
C
u
u
r
r
r
r
e
e
n
n
t
t
L
L
o
o
c
c
a
a
t
t
i
i
o
o
n
n
This tutorial shows how to Set Current Location on XCode.
F
F
o
o
r
r
S
S
i
i
m
m
u
u
l
l
a
a
t
t
o
o
r
r
Start Simulator
Debug
Location
Custom Location
Latitude: 16
Longitude: 21
OK
Custom Location
F
F
o
o
r
r
X
X
C
C
o
o
d
d
e
e
Product
Scheme
Edit Scheme
Run
Options
Allow Location Simulation: ON
Default Location: London, England
Close
Default Location: London, England
S
S
h
h
o
o
w
w
M
M
a
a
p
p
This tutorial shows how to show a Map.
ContentView.swift
import SwiftUI
import UIKit
import MapKit
struct ContentView: View {
var body: some View {
MapView()
}
}
struct MapView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView(frame: UIScreen.main.bounds)
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) { }
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Simulator
S
S
h
h
o
o
w
w
M
M
a
a
p
p
w
w
i
i
t
t
h
h
C
C
u
u
r
r
r
r
e
e
n
n
t
t
L
L
o
o
c
c
a
a
t
t
i
i
o
o
n
n
[
[
R
R
]
]
This tutorial shows how to show a Map with Current Location on it.
SwiftUI doesn't support Map View so we have to use MapKit and wrap it inside UIViewRepresentable.
ContentView.swift
import SwiftUI
import UIKit
import MapKit
struct ContentView: View {
var body: some View {
MapView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct MapView: UIViewRepresentable {
var locationManager = CLLocationManager()
func setupManager() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
}
func makeUIView(context: Context) -> MKMapView {
setupManager()
let mapView = MKMapView(frame: UIScreen.main.bounds)
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
}
Simulator (current location is not visible on simulator)
S
S
h
h
o
o
w
w
l
l
o
o
c
c
a
a
t
t
i
i
o
o
n
n
t
t
a
a
g
g
s
s
This tutorial shows how to show current location on a Map.
ContentView.swift
import SwiftUI
import UIKit
import MapKit
struct ContentView: View {
@State var checkpoints: [Checkpoint] = [
Checkpoint(title: "Da Nang", coordinate: .init(latitude: 16.047079, longitude: 108.206230)),
Checkpoint(title: "Ha Noi", coordinate: .init(latitude: 21.027763, longitude: 105.834160))
]
var body: some View {
MapViewAdvance(checkpoints: $checkpoints)
}
}
final class Checkpoint: NSObject, MKAnnotation {
let title: String?
let coordinate: CLLocationCoordinate2D
init(title: String?, coordinate: CLLocationCoordinate2D) {
self.title = title
self.coordinate = coordinate
}
}
struct MapViewAdvance: UIViewRepresentable {
@Binding var checkpoints: [Checkpoint]
func makeUIView(context: Context) -> MKMapView {
MKMapView()
}
func updateUIView(_ uiView: MKMapView, context: Context) {
uiView.addAnnotations(checkpoints)
}
}
struct ContentViewAdvance: View {
@State var checkpoints: [Checkpoint] = [
Checkpoint(title: "Da Nang", coordinate: .init(latitude: 16.047079, longitude: 108.206230)),
Checkpoint(title: "Ha Noi", coordinate: .init(latitude: 21.027763, longitude: 105.834160))
]
var body: some View {
MapViewAdvance(checkpoints: $checkpoints)
}
}
Simulator